using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;

using Outcoding.GraphGenerator.Entities;
using Outcoding.GraphGenerator.Composition;
using Outcoding.GraphGenerator.Util.Logging;
using Outcoding.GraphGenerator.Generator;

namespace Outcoding.GraphGenerator.MSOffice.MSPowerPoint {
    public partial class ThisAddIn {

        #region Class Members

        private const string TOOLBAR_NAME = "Graph Generator Bar";

        #region Private Members

        private ArrayList mToolbarControls; 
        
        #endregion

        #region Properties

        private ArrayList ToolbarControls {
            get {
                if (mToolbarControls == null)
                    mToolbarControls = new ArrayList();
                return mToolbarControls;
            }
            set {
                mToolbarControls = value;
            }
        }

        #endregion

        #endregion

        #region Private Methods

        private Office.CommandBar GetToolbar(string toolbarName) {
            foreach (Office.CommandBar toolbar in this.Application.CommandBars) {
                if (toolbar.Name.CompareTo(toolbarName) == 0) {
                    return toolbar;
                }
            }
            return null;
        }

        private Office.CommandBar AddToolbar() {
            Office.CommandBar toolbar = this.Application.CommandBars.Add(TOOLBAR_NAME, Office.MsoBarPosition.msoBarTop, missing, false);
            toolbar.Visible = true;

            //add controls to toolbar
            Office.CommandBarButton button = (Office.CommandBarButton)toolbar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, false);
            button.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
            button.Caption = "Import";
            button.Tag = "Import";
            button.DescriptionText = "Import graphs from XML";
            button.FaceId = 0;
            button.Picture = getImage();
            
            return toolbar;
        }

        private void SubscribeToToolbarEvents(Office.CommandBar toolbar) {
            foreach (Office.CommandBarControl control in toolbar.Controls) {
                if (control is Office.CommandBarButton) {
                    Office.CommandBarButton button = (Office.CommandBarButton)control;
                    button.Click += new Office._CommandBarButtonEvents_ClickEventHandler(BtnImport_Click);
                    this.ToolbarControls.Add(button);
                }
            }
        }

        private stdole.IPictureDisp getImage() {
            stdole.IPictureDisp tempImage = null;
            try {
                System.Drawing.Bitmap barImage = Resources.chart_bar_edit;
                tempImage = ConvertImage.Convert(barImage);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            return tempImage;
        }

        #endregion

        #region Handlers

        private void BtnImport_Click(Office.CommandBarButton cmdButton, ref bool cancel) {
            string path = System.Configuration.ConfigurationSettings.AppSettings["DataSourcePath"];
            if (!System.IO.File.Exists(path)) {
                OpenFileDialog openDialog = new OpenFileDialog();
                openDialog.DefaultExt = "*.xml";
                openDialog.Filter = "Chart Source Files|*.xml";
                if (openDialog.ShowDialog() == DialogResult.OK)
                    path = openDialog.FileName;
                else
                    path = null;
            }
            if (!string.IsNullOrEmpty(path)) {
                ImportChart(path);
            }
        }

        private void ImportChart(string path) {
            ChartGeneratorManager chartManager = new ChartGeneratorManager(path);
            try {
                chartManager.CreatePresentation();
            } catch (Exception exc) {
                LogManager.Instance.LogError("Error creating presentation", exc);
                MessageBox.Show("Error creating presentation. A log error entry has been created.");
            }
        }

        private void GraphGeneratorAddIn_Startup(object sender, System.EventArgs e) {
            //MessageBox.Show("hola mundo!");
            //toolbar handling
            Office.CommandBar toolbar = this.GetToolbar(TOOLBAR_NAME);
            if (toolbar != null)
                toolbar.Delete();
            toolbar = this.AddToolbar();
            this.SubscribeToToolbarEvents(toolbar);
        }

        private void GraphGeneratorAddIn_Shutdown(object sender, System.EventArgs e) {
            ToolbarControls = null;
        }

        #endregion

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup() {
            this.Startup += new System.EventHandler(GraphGeneratorAddIn_Startup);
            this.Shutdown += new System.EventHandler(GraphGeneratorAddIn_Shutdown);
        }

        #endregion
    }
}
